package cn.tedu.truetaste.controller;

import cn.tedu.truetaste.pojo.dto.DishesAddNewDTO;
import cn.tedu.truetaste.pojo.dto.DishesUpdateDTO;
import cn.tedu.truetaste.pojo.vo.DishesListItemVO;
import cn.tedu.truetaste.pojo.vo.DishesStandardVO;
import cn.tedu.truetaste.pojo.vo.SpuListItemVO;
import cn.tedu.truetaste.restful.JsonPage;
import cn.tedu.truetaste.service.IDishesService;
import cn.tedu.truetaste.util.WebConsts;
import cn.tedu.truetaste.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "菜品管理模块")
@Slf4j
@RestController
@RequestMapping("dishess")
public class DishesController {
    @Autowired
    private IDishesService dishesService;

    public DishesController() {
        log.info("创建控制器对象：DishesController");
    }

    // http://localhost:9989/dishess/add-new
    @ApiOperation("添加菜品")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(@Valid DishesAddNewDTO dishesAddNewDTO) {
        log.debug("开始处理【添加菜品】的请求，参数：{}", dishesAddNewDTO);
        dishesService.addNew(dishesAddNewDTO);
        return JsonResult.ok();
    }
    // http://localhost:9989/dishess/9527/delete
    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据id删除菜品")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "菜品ID", required = true, example = "9527", dataType = "long")
    public JsonResult<Void> delete(@Range(min = 1, max = 1000000, message = "删除菜品失败，菜品ID非法！")
                                   @PathVariable Long id) {
        log.debug("开始处理【根据id删除菜品】的请求，参数：{}", id);
        dishesService.delete(id);
        return JsonResult.ok();
    }
    // http://localhost:9989/dishess/9527/update
    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改菜品详情")
    @ApiOperationSupport(order = 300)
    public JsonResult<Void> updateInfoById(@PathVariable Long id, DishesUpdateDTO dishesUpdateDTO) {
        log.debug("开始处理【修改菜品详情】的请求，参数：{}", dishesUpdateDTO);
        dishesService.updateInfoById(id, dishesUpdateDTO);
        return JsonResult.ok();
    }

    // http://localhost:9989/dishess/9527
    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据id查询菜品详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParam(name = "id", value = "菜品ID", required = true, example = "9527", dataType = "long")
    public JsonResult<DishesStandardVO> getStandardById(@Range(min = 1, max = 1000000, message = "查询菜品详情失败，菜品ID非法！")
                                                       @PathVariable Long id) {
        log.debug("开始处理【根据id查询菜品详情】的请求，参数：{}", id);
        DishesStandardVO dishesStandardVO = dishesService.getStandardById(id);
        return JsonResult.ok(dishesStandardVO);
    }

    // http://localhost:9989/dishess/9527
    @GetMapping("/cy/{categoryId:[0-9]+}")
    @ApiOperation("根据类别id查询菜品数据详情")
    @ApiOperationSupport(order = 411)
    @ApiImplicitParam(name = "categoryId", value = "类别ID", required = true, example = "9527", dataType = "long")
    public JsonResult<DishesStandardVO> getCategoryId(@Range(min = 1, max = 1000000, message = "查询菜品详情失败，类别ID非法！")
                                                        @PathVariable Long categoryId) {
        log.debug("开始处理【根据类别id查询菜品详情】的请求，参数：{}", categoryId);
        DishesStandardVO dishesStandardVO = dishesService.getCategoryId(categoryId);
        return JsonResult.ok(dishesStandardVO);
    }

    // http://localhost:9989/dishess   分页 pageHelper
    @GetMapping("")
    @ApiOperation("" +
            "分页查询菜品列表")
    @ApiOperationSupport(order = 420)
    public JsonResult<JsonPage<DishesListItemVO>> list(
            @RequestParam(required = false,defaultValue = WebConsts.DEFAULT_PAGE)
                    Integer nowPage,
            @RequestParam(required = false,defaultValue = WebConsts.DEFAULT_PAGE_SIZE)
                    Integer pageNum) {
        log.debug("开始处理【查询菜品列表】的业务，页码{}，每页条数{}",nowPage,pageNum);
        return JsonResult.ok(dishesService.list(nowPage,pageNum));
    }

    @ApiOperation("根据菜品Id查询SpuDetail")
    @ApiOperationSupport(order = 500)
    @ApiImplicitParam(name = "dishesId", value = "dishesId",
            required = true, dataType = "long")
    @GetMapping("/list-by-dishes")
    public JsonResult<List<DishesListItemVO>> listBySpuDetailId(Long dishesId) {
        log.debug("开始处理【根据dishesId查询SpuDetail】的请求，参数：{}", dishesId);
        List<DishesListItemVO> list = dishesService.listBySpuDetailId(dishesId);
        return JsonResult.ok(list);
    }

}
